# 上传图片到图床并获取URL
import requests


def upload_image_to_imgbed(image_path):
    api_url = "https://picui.cn/api/v1/upload"
    headers = {
        "Authorization": "Bearer 970|rZ062rRWL5mQFAKmRejKiito0Acv24iDp3kGWMEv",  # token
        # "Content-Type": "multipart/form-data" # 不用管，会自动设置，否则会出错
    }
    files = {
        'file': open(image_path, 'rb'),
    }
    data = {
        'token': '970|rZ062rRWL5mQFAKmRejKiito0Acv24iDp3kGWMEv',  # token
        'permission': 1,  # 公开权限，根据需要修改
        # 'strategy_id': YOUR_STRATEGY_ID,  # 储存策略ID
        # 'album_id': YOUR_ALBUM_ID,  # 相册ID
        'expired_at': "2025-09-01 00:00:00"  # 设置过期时间，不能超过一年
    }
    response = requests.post(api_url, headers=headers, files=files, data=data)
    if response.status_code == 200:
        image_result = response.json()
        if image_result["status"]:
            return image_result["data"]["links"]["url"]
        else:
            print(f"图片上传成功但获取URL失败，描述信息: {image_result['message']}")
            return ""
    else:
        print(f"上传图片失败，状态码: {response.status_code}")
        return ""


if __name__ == "__main__":
    image_url = upload_image_to_imgbed(r'F:\桌面资料\毕业设计\图\预测结果可视化.png')
    print("图片:", image_url)
